//
//  UserManager.swift
//  MetaTest
//
//  Created by yhb on 2024/10/28.
//

import Foundation
class UserManager {
    static let shared = UserManager()
    var questionList: [String] = []
    var answerList: [String] = []
    
    enum CacheKeys: String {
        case QUESTIONS = "questionList"
        case ANSWERS = "answerList"
        
    }
    private init() {}
    
    func saveQuestions(questionList: [String]) {
        UserDefaults.standard.set(questionList, forKey: CacheKeys.QUESTIONS.rawValue)
        UserDefaults.standard.synchronize()
    }
    func removeQuestions() {
        UserDefaults.standard.removeObject(forKey: CacheKeys.QUESTIONS.rawValue)
        UserDefaults.standard.synchronize()
    }
    func getQuestions() -> [String] {
            if let savedQuestions = UserDefaults.standard.array(forKey: CacheKeys.QUESTIONS.rawValue) as? [String] {
                return savedQuestions
            }
            return []
        }
    func saveAnswers(answers: [String]) {
            UserDefaults.standard.set(answers, forKey: CacheKeys.ANSWERS.rawValue)
            UserDefaults.standard.synchronize()
        }
        
        func removeAnswers() {
            UserDefaults.standard.removeObject(forKey: CacheKeys.ANSWERS.rawValue)
            UserDefaults.standard.synchronize()
        }
        
        func getAnswers() -> [String] {
            if let savedAnswers = UserDefaults.standard.array(forKey: CacheKeys.ANSWERS.rawValue) as? [String] {
                return savedAnswers
            }
            return []
        }
    func loadFromUserDefaults() {
        questionList = UserDefaults.standard.array(forKey: CacheKeys.QUESTIONS.rawValue) as? [String] ?? []
        answerList = UserDefaults.standard.array(forKey: CacheKeys.ANSWERS.rawValue) as? [String] ?? []
    }
}
